package com.tutu.user.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.tutu.common.GlobalConstants;
import com.tutu.user.dao.SysUserGroupMebersMapper;
import com.tutu.user.entity.SysUserGroupMebers;
import com.tutu.user.entity.SysUserGroupMebersExample;
import com.tutu.user.entity.SysUserGroupMebersExample.Criteria;
import com.tutu.user.service.ISysUserGroupMebersService;

@Service("sysUserGroupMebersService")
public class SysUserGroupMebersServiceImpl implements ISysUserGroupMebersService {
	
	@Resource
	private SysUserGroupMebersMapper sysUserGroupMebersMapper;
	
	@Override
	public void saveSysUserGroupMebers(SysUserGroupMebers sysUserGroupMebers) {
		this.sysUserGroupMebersMapper.insert(sysUserGroupMebers);
		
	}

	@Override
	public void saveSysUserGroupMebersMany(
			List<SysUserGroupMebers> sysUserGroupMebersList) {
		if(CollectionUtils.isEmpty(sysUserGroupMebersList))
			return;
//		清除用户角色
		SysUserGroupMebersExample sysUserGroupMebersExample = new SysUserGroupMebersExample();
		sysUserGroupMebersExample.createCriteria().andUserIdEqualTo(sysUserGroupMebersList.get(0).getUserId());
		this.sysUserGroupMebersMapper.deleteByExample(sysUserGroupMebersExample);
		for(SysUserGroupMebers sysUserGroupMebers: sysUserGroupMebersList){
			saveSysUserGroupMebers(sysUserGroupMebers);
		}
	}

	@Override
	public List<SysUserGroupMebers> getSysUserGroupMebers(
			SysUserGroupMebers sysUserGroupMebers) {
		return this.sysUserGroupMebersMapper.selectMebersAndGroup(sysUserGroupMebers);
	}

	@Override
	public List<SysUserGroupMebers> getSysUserGroupMebersByGroup(
			SysUserGroupMebers sysUserGroupMebers) {
		return this.sysUserGroupMebersMapper.getSysUserGroupMebersByGroup(sysUserGroupMebers);
	}

	@Override
	public void updateLeader(SysUserGroupMebers sysUserGroupMebers) {
//		用户组中负责人初始化
		SysUserGroupMebersExample sysUserGroupMebersExample = new SysUserGroupMebersExample();
		Criteria criteria = sysUserGroupMebersExample.createCriteria();
		criteria.andGroupIdEqualTo(sysUserGroupMebers.getGroupId());
		SysUserGroupMebers record = new SysUserGroupMebers();
		record.setIsLeader(GlobalConstants.FLAG_DISENABLE);
		this.sysUserGroupMebersMapper.updateByExampleSelective(record, sysUserGroupMebersExample);
//		设置负责人
		this.sysUserGroupMebersMapper.updateByPrimaryKeySelective(sysUserGroupMebers);
	}

}
